<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <blockquote class="layui-elem-quote layui-text">
            在这里设置的公告会出现在管理员和辅导员的后台个人中心
        </blockquote>
        <div class="layui-form" action="">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>公告列表</legend>
            </fieldset>

            <script type="text/html" id="notitoolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                </div>
            </script>

            <table class="layui-hide" id="notiTableId" lay-filter="notiTableFilter"></table>

            <script type="text/html" id="notiTableBar">
                <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            </script>

        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'miniPage', 'element'], function() {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;


        //表格初始化
        table.render({
            elem: '#notiTableId',
            url:BasePath+'api/Notification/GetNotices',
            toolbar: '#notitoolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {title: '操作', minWidth: 100, toolbar: '#notiTableBar', align: "center"},
                {field: 'id', width: 80, title: 'ID'},
                {field: 'title', width: 150, title: '公告标题'},
                {field: 'content', title: '公告内容', minWidth: 280},
                {field: 'creatorName', title: '发布人', minWidth: 150},
                {field: 'createTime', width: 135, title: '创建时间', sort: true}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: false,
            skin: 'line'
        });

        // toolbar事件
        table.on('toolbar(notiTableFilter)', function(obj) {
            if (obj.event === 'add') { // 监听添加操作
                var content = miniPage.getHrefContent('page/admin/notification-setting/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加公告',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function() {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') { // 监听删除操作
                var checkStatus = table.checkStatus('notiTableId'),
                    data = checkStatus.data;
                if (data.length === 0) {
                    layer.alert('未选择要删除的数据', {
                        title: '操作不合法'
                    });
                    return;
                }
                var deletedItemCount = 0;
                data.forEach(function(deleteData) {
                    if (deleteNoti(deleteData.id)) {
                        deletedItemCount++;
                    }
                });
                layer.alert('成功删除' + deletedItemCount + '个数据,' + (data.length - deletedItemCount) + '条删除失败，如果存在删除失败，请尝试刷新界面重试', {
                    title: '操作结果'
                });
                table.reload('notiTableId', {}, 'data');
                console.log(data)
            }
        });

        //操作点击事件
        table.on('tool(notiTableFilter)', function(obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                console.log(data)
                var content = miniPage.getHrefContent('page/admin/classes-setting/edit.html' + '?id=' + data.id);
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function() {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function(index) {
                    deleteNoti(data.id)
                    table.reload('notiTableId', {}, 'data');
                    layer.close(index);
                });
            }
        });

        //删除公告函数
        function deleteNoti(id) {
            var result=false;
            jq1111.ajax({
                type: "POST",
                crossDomain: true,
                url: BasePath+"api/Notification/"+id+"/DeleteNotice",
                contentType: "application/json",
                dataType: "json",
                async:false,
                xhrFields: {
                    withCredentials: true
                },
                success: function(message) {
                    result = true;
                },
                error: function(message) {
                    console.log(message.responseJSON);
                    result = false;
                },
            });
            return result;
        }




    })
</script>